from model.hotfix_module import HotfixModule
from model.hotfix_version import HotfixVersion
from mutils.zip import zip_dir
import os
from typing import List


class ModuleManager:
    def __init__(self):
        self.modules: List[HotfixModule] = []

    def init(self):
        self.modules.append(
            HotfixModule(
                "wechat-token",
                HotfixVersion.Parse("1.0.0"),
                "from main import run\nrun()",
            )
        )
        
        self.modules.append(
            HotfixModule(
                "su-account",
                HotfixVersion.Parse("1.0.0"),
                "from main import run\nrun()",
            )
        )

        self.modules.append(
            HotfixModule(
                "ydt-token",
                HotfixVersion.Parse("1.0.0"),
                "from main import run\nrun()",
            )
        )
        
        for module in self.modules:
            if not os.path.exists(module.module_path()):
                os.makedirs(module.module_path())
            # if os.path.exists(module.zip_path()):
            #     continue
            zip_dir(module.module_path(), module.zip_path())

    def get_laetst_module(self, name):
        matched_modules = []

        for module in self.modules:
            if module.name == name:
                matched_modules.append(module)

        if len(matched_modules) == 0:
            return None

        matched_modules.sort(key=lambda x: x.version, reverse=True)

        return matched_modules[0]
